package com.nf.health.app.intervention_details.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.customview.MyFlowLayout;
import com.nf.health.app.glide.GlideUtil;
import com.nf.health.app.intervention_details.bean.UserHealthInfoBean;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.DrawableUtils;
import com.nf.health.app.utils.Util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 恩福 on 2016/11/10.
 * 个人健康信息汇总 Adapter
 *
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class UserHealthInfoAdapter extends RecyclerView.Adapter {
    private static final int TYPE_USER_INFO = 0;

    private static final int TYPE_IMPORTANT_TARGET_RESULT = 1;
    private static final int TYPE_LIFE_STYLE_RESULT = 2;

    private static final int TYPE_PERSONAL_ILLNESS = 3;
    private static final int TYPE_FAMILY_ILLNESS = 4;

    private static final int TYPE_FOOTER = 5;
    private UserHealthInfoBean userHealthInfoBean;
    private Context mContext;

    public UserHealthInfoAdapter(Context context, UserHealthInfoBean userHealthInfoBean) {
        this.mContext = context;
        this.userHealthInfoBean = userHealthInfoBean;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE_USER_INFO:
                view = LayoutInflater.from(mContext).inflate(R.layout.user_health_info_list_item1, parent, false);
                return new UserInfoHolder(view);
            case TYPE_IMPORTANT_TARGET_RESULT: //重要指标检测结果
                view = LayoutInflater.from(mContext).inflate(R.layout.user_health_info_list_item2, parent, false);
                return new ResultHolder(view,TYPE_IMPORTANT_TARGET_RESULT);
            case TYPE_LIFE_STYLE_RESULT: //生活方式评价结果
                view = LayoutInflater.from(mContext).inflate(R.layout.user_health_info_list_item2, parent, false);
                return new ResultHolder(view,TYPE_LIFE_STYLE_RESULT);
            case TYPE_PERSONAL_ILLNESS:
                view = LayoutInflater.from(mContext).inflate(R.layout.user_health_info_list_item3, parent, false);
                return new IllnessHolder(view,TYPE_PERSONAL_ILLNESS);
            case TYPE_FAMILY_ILLNESS:
                view = LayoutInflater.from(mContext).inflate(R.layout.user_health_info_list_item3, parent, false);
                return new IllnessHolder(view,TYPE_FAMILY_ILLNESS);
            case TYPE_FOOTER:
                view = LayoutInflater.from(mContext).inflate(R.layout.layout_foot_details, parent, false);
                return new FooterHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(userHealthInfoBean == null){
            return;
        }
        if (holder instanceof UserInfoHolder) {
            UserInfoHolder userInfoHolder = (UserInfoHolder) holder;
            UserHealthInfoBean.UserBean userBean = userHealthInfoBean.getUser();
            userInfoHolder.tvUserInfoLeft.setText("姓名:"+userBean.getName()+"\n性别:"+ Util.getSex(userBean.getSex())+"\n年龄:"+userBean.getAge());
            userInfoHolder.tvUserInfoRight.setText("医生:"+userBean.getDname()+"\n手机:"+userBean.getPhone()+"\n疾病:"+userBean.getDisease());
            GlideUtil.loadNetImg2(mContext, userInfoHolder.ivUserHimg, userBean.getHimg(), R.drawable.default_head);
        }
        if (holder instanceof ResultHolder) {
            ResultHolder resultHolder = (ResultHolder) holder;

        }
        if (holder instanceof IllnessHolder) {
            IllnessHolder illnessHolder = (IllnessHolder) holder;
        }
    }

    @Override
    public int getItemCount() {
        return 6;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return TYPE_USER_INFO;
            case 1:
                return TYPE_IMPORTANT_TARGET_RESULT;
            case 2:
                return TYPE_LIFE_STYLE_RESULT;
            case 3:
                return TYPE_PERSONAL_ILLNESS;
            case 4:
                return TYPE_FAMILY_ILLNESS;
            case 5:
                return TYPE_FOOTER;
        }
        return super.getItemViewType(position);
    }

    class UserInfoHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_user_himg)
        CircleImageView ivUserHimg;
        @Bind(R.id.tv_user_info_left)
        TextView tvUserInfoLeft;
        @Bind(R.id.tv_user_info_right)
        TextView tvUserInfoRight;

        public UserInfoHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ResultHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_subtitle)
        TextView tvSubtitle;
        @Bind(R.id.recycler_view_user_health_info_item2)
        RecyclerView recyclerViewUserHealthInfoItem2;
        SchemeResultAdapter adapter;
        private int itemType;
        public ResultHolder(View itemView,int itemType) {
            super(itemView);
            this.itemType = itemType;
            ButterKnife.bind(this, itemView);
            initView();
        }

        private void initView() {
            recyclerViewUserHealthInfoItem2.setLayoutManager(new LinearLayoutManager(mContext));
            recyclerViewUserHealthInfoItem2.setItemAnimator(new DefaultItemAnimator());
            recyclerViewUserHealthInfoItem2.setFocusable(false); // 防止自己滑动
            if(itemType == TYPE_IMPORTANT_TARGET_RESULT) { //加载targetBean
                tvSubtitle.setText("重要指标检测结果");
                adapter = new SchemeResultAdapter(mContext,userHealthInfoBean.getMonitordata());
                recyclerViewUserHealthInfoItem2.setAdapter(adapter);
            }
            if(itemType == TYPE_LIFE_STYLE_RESULT){
                tvSubtitle.setText("生活方式评价结果");
                adapter = new SchemeResultAdapter(mContext,userHealthInfoBean.getLifeway());
                recyclerViewUserHealthInfoItem2.setAdapter(adapter);
            }
        }
    }

    class IllnessHolder extends RecyclerView.ViewHolder {
        private final int itemType;
        @Bind(R.id.tv_subtitle)
        TextView tvSubtitle;
        @Bind(R.id.my_flow_layout)
        MyFlowLayout flowLayout;

        public IllnessHolder(View itemView,int itemType) {
            super(itemView);
            this.itemType = itemType;
            ButterKnife.bind(this, itemView);
            if(itemType == TYPE_PERSONAL_ILLNESS){
                tvSubtitle.setText("个人疾病");
                String illness = userHealthInfoBean.getSickshospital().getValue();
                String illnessOther = userHealthInfoBean.getSickshospital().getOther();
                if(!TextUtils.isEmpty(illness) || !TextUtils.isEmpty(illnessOther)){
                    String[] ills = illness.split(",");
                    String[] illsOth = illnessOther.split(",");
                    initTipTextView(flowLayout,concat(ills,illsOth));
                }
            }
            if(itemType == TYPE_FAMILY_ILLNESS){
                tvSubtitle.setText("家族疾病");
                String illness = userHealthInfoBean.getFamilyshospital().getValue();
                String illnessOther = userHealthInfoBean.getFamilyshospital().getOther();
                if(!TextUtils.isEmpty(illness) || !TextUtils.isEmpty(illnessOther)) {
                    String[] ills = illness.split(",");
                    String[] illsOth = illnessOther.split(",");
                    initTipTextView(flowLayout, concat(ills,illsOth));
                }
            }
        }

        private void initTipTextView(MyFlowLayout flowLayout,String[] ills) {
            int padding = DensityUtil.dip2px(mContext, 12);
            for (int i = 0; i < ills.length; i++) {
                final String keyword = ills[i];
                if(TextUtils.isEmpty(keyword)){
                    continue;
                }
                TextView view = new TextView(mContext);
                view.setText(keyword);
                view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);// 18sp
                view.setGravity(Gravity.CENTER);

                // 生成随机颜色
                Random random = new Random();
                int r = 30 + random.nextInt(200);
                int g = 30 + random.nextInt(200);
                int b = 30 + random.nextInt(200);
                view.setTextColor(Color.rgb(r, g, b));
                GradientDrawable bgNormal = DrawableUtils.getGradientDrawable(Color.rgb(r, g, b), DensityUtil.dip2px(mContext, 20));
                view.setBackgroundDrawable(bgNormal);
                view.setPadding(padding, DensityUtil.dip2px(mContext, 3), padding, DensityUtil.dip2px(mContext, 3));
                ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                marginLayoutParams.setMargins(DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 10), DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 10));// childView的间距
                flowLayout.addView(view, marginLayoutParams);
            }
        }
    }

    /**
     * 将两个数据合并成一个
     * @param a
     * @param b
     * @return
     */
    String[] concat(String[] a, String[] b) {
        String[] c= new String[a.length+b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }
    class FooterHolder extends RecyclerView.ViewHolder {

        public FooterHolder(View itemView) {
            super(itemView);
        }
    }


}
